﻿using NHibernate;
using NUnit.Framework;
using FluentNHibernate.Cfg.Db;
using HibernateConfig = NHibernate.Cfg;
using FluentNHibernate;
using NHibernate.Tool.hbm2ddl;
using System;
using System.IO;

namespace NMA.Infrastructure.NHibernate.Tests
{
    using NMA.Infrastructure.NHibernate.Mapping;    

    /// <summary>
    /// http://blogs.hibernatingrhinos.com/nhibernate/archive/2008/11/18/legacy-db-and-one-to-one-relations.aspx
    /// http://nhforge.org/blogs/nhibernate/archive/2008/09/06/a-fluent-interface-to-nhibernate-part-3-mapping-relations.aspx
    /// http://marekblotny.blogspot.com/2009/03/fluent-nhibernate-and-inheritance.html
    /// http://moleseyhill.com/blog/2009/12/02/fluent-nhibernate-unit-of-work-pattern/
    /// http://blog.vuscode.com/malovicn/archive/2008/08/31/fluent-nhibernate-nhibernate-without-configuration-files.aspx
    /// http://devlicio.us/blogs/casey
    /// </summary>
    [TestFixture]
    public class TestBaseClass
    {
        public static ISessionFactory SessionFactory { get; set; }

        [SetUp]
        public void SetupContext()
        {
            Before_each_test();
        }

        [TearDown]
        public void TeardownContext()
        {
            After_each_test();
        }

        protected virtual void Before_each_test()
        {
            try
            {
                IPersistenceConfigurer persistenceConfigurer =
                MsSqlConfiguration
                    .MsSql2005
                    .ConnectionString("Data Source=.;Initial Catalog=NMA; Integrated Security=True")
                    .ShowSql();

                // initialize nhibernate with persistance configurer properties
                HibernateConfig.Configuration cfg = persistenceConfigurer
                    .ConfigureProperties(new HibernateConfig.Configuration());

                // add mappings definition to nhibernate configuration
                var persistenceModel = new PersistenceModel();

                //persistenceModel.AddMappingsFromAssembly(typeof(UserMap).Assembly);
                persistenceModel.AddMappingsFromAssembly(typeof(CategoryMap).Assembly);
                //persistenceModel.AddMappingsFromAssembly(typeof(NewsMap).Assembly);
                //persistenceModel.AddMappingsFromAssembly(typeof(PollMap).Assembly);

                persistenceModel.Configure(cfg);

                // build schema
                //BuildSchema(cfg);

                // set session factory field which is to be used in tests
                SessionFactory = cfg.BuildSessionFactory();
            }
            catch (Exception ex)
            {                
                throw ex;
            }            
        }

        protected virtual void After_each_test()
        {
            if (SessionFactory != null)
                GC.SuppressFinalize(SessionFactory);
        }

        private void BuildSchema(HibernateConfig.Configuration config)
        {
            string DbFile = AppDomain.CurrentDomain.BaseDirectory + "\\NMASchema.xml";

            // delete the existing db on each run
            if (File.Exists(DbFile))
                File.Delete(DbFile);

            // this NHibernate tool takes a configuration (with mapping info in)
            // and exports a database schema from it
            new SchemaExport(config)
              .Create(false, true);
        }

    }
}
